package com.manager.schedule.service.impl;

import com.manager.common.dao.SuperDao;
import com.manager.common.utils.ObjectUtils;
import com.manager.common.vo.PageVO;
import com.manager.model.DockerDeploy;
import com.manager.model.SchedulingLogs;
import com.manager.model.SchedulingStrategy;
import com.manager.model.SchedulingStrategyDockerContainer;
import com.manager.schedule.service.SchedulingService;
import jakarta.transaction.Transactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SchedulingServiceImpl extends SuperDao<Object> implements SchedulingService {

    @Override
    public PageVO getSchedulePage(SchedulingStrategy model) throws Exception{
        List<Object> params  = new ArrayList<>();
        String sql = " SELECT t.*,GROUP_CONCAT(tdd.container_name) as priority,GROUP_CONCAT(tdd.table_id) as containerIds FROM tb_scheduling_strategy t " +
                "LEFT JOIN tb_scheduling_strategy_docker_container tsc on t.table_id = tsc.scheduling_strategy_table_id " +
                "LEFT JOIN tb_docker_deploy tdd on tsc.docker_deploy_table_id = tdd.table_id where 1 = 1  ";
        if(StringUtils.isNotEmpty(model.getStrategyName())){
            sql += " and t.strategy_name like # ";
            params.add("%" + model.getStrategyName() + "%");
        }

        sql = ObjectUtils.replaceIndexByList(sql, params);
        sql += " GROUP BY t.table_id ";
        List<SchedulingStrategy> list = this.getListByColumSQL(sql,params,model.getStart(),model.getLimit(),SchedulingStrategy.class);
        int sum = this.getListCountSql("select count(*) from ( " + sql + ")temp", params.toArray(), "");
        int count = (int)Math.ceil(Double.valueOf(sum)/Double.valueOf(model.getLimit()));

        return PageVO.create(model.getStart(),count,ObjectUtils.getJSONArray(new String[]{"schedulingStrategyDockerContainerList"},list));
    }

    @Override
    public PageVO getScheduleLogPage(SchedulingLogs model) throws Exception {
        List<Object> params  = new ArrayList<>();
        String sql = " from SchedulingLogs t where 1 = 1  ";
        if(StringUtils.isNotEmpty(model.getStrategyName())){
            sql += " and t.strategyName like # ";
            params.add("%" + model.getStrategyName() + "%");
        }
        sql = ObjectUtils.replaceIndexByList(sql, params);
        List<SchedulingLogs> list = this.getList(sql,params.toArray(),model.getStart(),model.getLimit());
        int sum = this.getListCount("select count(*) " + sql, params.toArray(), "");
        int count = (int)Math.ceil(Double.valueOf(sum)/Double.valueOf(model.getLimit()));

        return PageVO.create(model.getStart(),count,list);
    }

    @Override
    public void addSchedule(SchedulingStrategy model) {
        model.setHasEnable(1);
        this.addT(model);
        List<String> containerIdList = ObjectUtils.jsonArray2Bean(model.getContainerIds(), String.class);
        if(containerIdList != null && containerIdList.size() > 0){
            for(int i = 0;i < containerIdList.size();i++){
                DockerDeploy dockerDeploy = this.getT(DockerDeploy.class, containerIdList.get(i));
                SchedulingStrategyDockerContainer schedulingStrategyDockerContainer = new SchedulingStrategyDockerContainer();
                schedulingStrategyDockerContainer.setSchedulingStrategy(model);
                schedulingStrategyDockerContainer.setDockerDeploy(dockerDeploy);
                schedulingStrategyDockerContainer.setContainerSort(i);
                this.addT(schedulingStrategyDockerContainer);
            }
        }
    }

    @Override
    public void updateSchedule(SchedulingStrategy model) {
        SchedulingStrategy schedulingStrategy = this.getT(SchedulingStrategy.class,model.getId());
        BeanUtils.copyProperties(model,schedulingStrategy,new String[]{"id","schedulingStrategyDockerContainerList"});
        this.batchDelete(schedulingStrategy.getSchedulingStrategyDockerContainerList());
        this.updateT(schedulingStrategy);
       List<String> containerIdList = ObjectUtils.jsonArray2Bean(model.getContainerIds(), String.class);
        if(containerIdList != null && containerIdList.size() > 0){
            for(int i = 0;i < containerIdList.size();i++){
                DockerDeploy dockerDeploy = this.getT(DockerDeploy.class, containerIdList.get(i));
                SchedulingStrategyDockerContainer schedulingStrategyDockerContainer = new SchedulingStrategyDockerContainer();
                schedulingStrategyDockerContainer.setSchedulingStrategy(schedulingStrategy);
                schedulingStrategyDockerContainer.setDockerDeploy(dockerDeploy);
                schedulingStrategyDockerContainer.setContainerSort(i);
                this.addT(schedulingStrategyDockerContainer);
            }
        }
    }

    @Override
    public void deleteSchedule(SchedulingStrategy model) {
        SchedulingStrategy schedulingStrategy = this.getT(SchedulingStrategy.class,model.getId());
        this.delete(schedulingStrategy);
    }

    @Override
    public SchedulingStrategy getSchedulingStrategy(String id) {
        SchedulingStrategy schedulingStrategy = this.getT(SchedulingStrategy.class,id);
        return schedulingStrategy;
    }

    @Override
    public void stopScheduling(String id) {
        SchedulingStrategy schedulingStrategy = this.getT(SchedulingStrategy.class,id);
        schedulingStrategy.setHasEnable(0);
        this.updateT(schedulingStrategy);
    }

    @Override
    public void startScheduling(String id) {
        SchedulingStrategy schedulingStrategy = this.getT(SchedulingStrategy.class,id);
        schedulingStrategy.setHasEnable(1);
        this.updateT(schedulingStrategy);
    }

    @Override
    public List<SchedulingStrategy> getSchedulingStrategies() {
        String sql = " from  SchedulingStrategy t ";
        List<SchedulingStrategy> list = this.getList(sql,0,0,"");
        return list;
    }
}
